/**
 * @tag controllers, home
 */
jQuery.Controller.extend('Bugstat.Controllers.PageDisplayController',
/* @Static */
{
    onDocument: true
},
/* @Prototype */
{
	/**
     * When the page loads, retrieve page metadata
     */
	load: function() 
	{
		Bugstat.Models.PageMetadata.findAll({}, this.callback('handleMetadataLoaded'), this.callback('handleError') ) ;
	}, 
	
	/**
     * When controller is ready, attach primary controllers
     */	
	ready : function()
	{
		if ( this.browserCompatible() )
		{			
			// Block user interface
			$.blockUI( { message: $.View('//bugstat/views/page_display/loading.ejs'),
					     css: { border: '', cursor: ''} } );
		}
		else
		{
			this.handleBrowserNotCompatible();
		}
	},
	
	/**
     * Update page according to recieved metadata object
     * @param {Array} metadatas An array of Metadata objects.
     */
    handleMetadataLoaded: function(metadata) 
	{
		if ( metadata )
		{
			$('#page-display').html( '//bugstat/views/page_display/init.ejs', this );
			this.makeSortable();
			
			document.title = metadata.name;

			$(".mainTitle").html( metadata.name );
			$(".subTitle").html( metadata.subName );
		
			// Attach important dates controller and these dates
			$('body').bugstat_important_dates();
			$('body').bugstat_important_dates( "loadDates", 
				 		 					   this.callback('handleImportantDatesLoaded'),
				 							   this.callback('handleError') );			
		}
		else
		{
			this.handleError(0, 0, "cant_load_page_metadata");
		}
    },

	handleImportantDatesLoaded: function() 
	{		
		// Attach list controller and load descriptors
	    $('#report_list').bugstat_report_list();
		$('#report_list').bugstat_report_list( "loadDescriptors", 
			 								   this.callback('handleReportListLoaded'),
			 								   this.callback('handleError') );

	},
	
	handleReportListLoaded: function() 
	{					
		$.unblockUI( { fadeOut: 300 } );
	},
		
	handleError : function(request, error, status) 
	{
		$('#page-display').hide();
		
		$.blockUI( { message: $.View('//bugstat/views/page_display/loading_error.ejs', status),
				     css: { border: '', cursor: ''} } );
	},
	
	handleBrowserNotCompatible : function() 
	{
		$('#page-display').hide();
		
		$.blockUI( { message: this.badBrowserMessage,
				     css: { border: '', cursor: ''} } );
	},
	
	browserCompatible : function() 
	{
		// Right now we do not support any Internet Explorers that are 
		// prior to IE8
		if ( $.browser.msie  && parseInt($.browser.version) == 6 )
		{
			return false;
		}
		
		// We do not support Chrome when running from a local hard drive
		// (it works when loading view from the server)
		var appVersion = navigator.appVersion;		
		if ( appVersion.indexOf('Chrome') >= 0) 
		{
			var location = window.location.toString();
			return location.indexOf("http") == 0;
		}
		
		return true;
	},
	
	makeSortable: function() {
		$("#report_graphs").sortable( { handle: ".report-head-stripe",
											scroll: true,	
											opacity: 0.8,		
											forcePlaceholderSize: true,	
											containment: 'document',
											placeholder: 'dragged-report-placeholder'
										   } );
	},
	
	badBrowserMessage: "<div class='loading_error'>" +
							"<h3>" +
							"  Bugstat does not support this browser." +
							"</h3>" +
							"<p>" +
							"	Bugstat is not able to display your reports because your browser is not yet supported." +
							"   Bugstat has been tested with following browsers: Internet Explorer 8, Firefox 3, Safari 5 and Opera." +
							"</p>" +
						"</div>"
} )